package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.dto.admin.exampaper.ExamPaperPageDTO;
import com.mindskip.xzs.domain.dto.admin.exampaper.ExamPaperWithQuestionDTO;
import com.mindskip.xzs.domain.entity.ExamPaper;
import com.mindskip.xzs.domain.vo.admin.exampaper.ExamPaperWithQuestionVO;
import com.mindskip.xzs.utility.RestResponse;
import com.mindskip.xzs.domain.vo.admin.exampaper.ExamPaperVO;
import com.mindskip.xzs.service.ExamPaperService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "试卷管理接口")
@RestController("AdminExamPaperController")
@RequestMapping(value = "/api/admin/exam/paper")
public class ExamPaperController {

    private final ExamPaperService examPaperService;

    public ExamPaperController(ExamPaperService examPaperService) {
        this.examPaperService = examPaperService;
    }

    @PostMapping("/page")
    public RestResponse<PageInfo<ExamPaperVO>> pageList(@RequestBody ExamPaperPageDTO examPaperPageDTO) {

        PageInfo<ExamPaperVO> pageInfo = examPaperService.getExamPaperByPage(examPaperPageDTO);
        return RestResponse.ok(pageInfo);
    }

    @PostMapping("/select/{id}")
    public RestResponse<ExamPaperWithQuestionVO> select(@PathVariable Integer id) {

        ExamPaperWithQuestionVO examPaperWithQuestion = examPaperService.getExamPaperWithQuestion(id);
        return RestResponse.ok(examPaperWithQuestion);
    }

    @PostMapping("/edit")
    public RestResponse<ExamPaperWithQuestionVO> edit(@RequestBody @Valid ExamPaperWithQuestionDTO examPaperWithQuestionDTO) {

        ExamPaper examPaper = examPaperService.insertOrUpdateExamPaper(examPaperWithQuestionDTO);
        ExamPaperWithQuestionVO examPaperWithQuestion = examPaperService.getExamPaperWithQuestion(examPaper.getId());
        return RestResponse.ok(examPaperWithQuestion);
    }

    @PostMapping("/delete/{id}")
    public RestResponse<Object> delete(@PathVariable Integer id) {

        examPaperService.softDelete(id);
        return RestResponse.ok();
    }

    @PostMapping("/taskExamPage")
    public RestResponse<PageInfo<ExamPaperVO>> taskExamPage(@RequestBody ExamPaperPageDTO examPaperPageDTO) {

        PageInfo<ExamPaperVO> pageInfo = examPaperService.taskExamPage(examPaperPageDTO);
        return RestResponse.ok(pageInfo);
    }

}
